/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-09-04 16:48:41
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-10-15 11:32:25
 * @FilePath: /react18-vite-admin/src/service/useRules.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import i18n from 'i18next';
import type { FormRule } from 'antd';

export const useRules = () => {
    const requiredRule: FormRule[] = [
        { required: true, message: i18n.t('rules.required') }
    ];

    const mobileRule: FormRule[] = [
        { min: 11, message: i18n.t('rules.mobile') }
    ];

    const resourceCodeRule: FormRule[] = [
        {
            validator: (rule: any, value: string, callback: any) => {
                if (value) {
                    const regex = /^\d-\d{2}-\d{3}-\d+$/;
                    if (!regex.test(value)) {
                        callback(
                            new Error(
                                '资源代码用-分隔多项数字，第一项一位，第二项两位，第三项三位，第四项不限位数'
                            )
                        );
                    }
                }
                callback();
            }
        }
    ];

    const scriptContentRule: FormRule[] = [
        {
            validator: (rule: any, value: string, callback: any) => {
                if (value) {
                    const regex =
                        /^function\s+[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(\s*[a-zA-Z_$][a-zA-Z0-9_$]*\s*\)\s*\{[^}]*\}$/;
                    if (!regex.test(value)) {
                        callback(
                            new Error(`
                            脚本内容格式错误，请输入符合如下格式的脚本内容：
                            function xxx(aaa) {
                                // 脚本内容
                            }
                            xxx为任意函数名
                            aaa为任意参数名
                        `)
                        );
                    }
                }
                callback();
            }
        }
    ];

    const cronRule: FormRule[] = [
        {
            validator: (rule: any, value: string, callback: any) => {
                if (value) {
                    const regex =
                        /^(\*|[0-5]?\d|\?[0-9]?)([\s,]+(\*|[0-5]?\d|[1-2]?\d-[3-4]?\d|JAN?|FEB?|MAR?|APR?|MAY?|JUN?|JUL?|AUG?|SEP?|OCT?|NOV?|DEC?|SUN?|MON?|TUE?|WED?|THU?|FRI?|SAT?)){4,5}$/;
                    if (!regex.test(value)) {
                        callback(
                            new Error(`
                            cron表达式格式为：秒 分 时 日 月 周
                            秒：0-59
                            分：0-59
                            时：0-23
                            日：1-31
                            月：1-12
                            周：0-6
                            ?：不指定
                            *：全部
                            ,：指定
                            -：区间
                            /：步进
                        `)
                        );
                    }
                }
                callback();
            }
        }
    ];

    const centerDataFilePathRule: FormRule[] = [
        {
            validator: (rule: any, value: string, callback: any) => {
                if (value) {
                    const regex =
                        /^\/data\/attachment\/datacenter\/center_resource\//;
                    if (!regex.test(value)) {
                        callback(
                            new Error(
                                `文件路径以/data/attachment/datacenter/center_resource/开头`
                            )
                        );
                    }
                }
                callback();
            }
        }
    ];
    return {
        requiredRule,
        mobileRule,
        resourceCodeRule,
        scriptContentRule,
        cronRule,
        centerDataFilePathRule
    };
};
